﻿using System;
using AppLoger.Abstract;
using Core.DependencyInjection;

namespace DounloderV2ConsoleApp
{
    class AppRun
    {
        /// <summary>
        /// Error exit message.
        /// </summary>
        private const string ExitErrorMessage = "Application is exit with error. Press Enter to continue";

        /// <summary>
        /// Exit message.
        /// </summary>
        private const string ExitMessage = "Press any key to continue";

        /// <summary>
        /// Global loger.
        /// </summary>
        private static IAppLoger _appLoger;

        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;
            NinjectFactory.Wire(new ApplicationModule());
            _appLoger = NinjectFactory.Resolve<AppLoger.Implementation.AppLoger>();

            Console.WriteLine(ExitMessage);
            Console.ReadLine();
        }

        private static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                var ex = (Exception)e.ExceptionObject;
                Console.WriteLine(ex.Message);
                _appLoger.Fatal(ex);
            }
            finally
            {
                Console.WriteLine(ExitErrorMessage);
                Console.ReadLine();
                Environment.Exit(1);
            }
        }
    }
}
